package com.sskj.mine.ui.activity;

import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.base.App;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.component.DaggerUserDataComponent;
import com.sskj.mine.presenter.UnBindEmailPresenter;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 解除邮箱绑定
 */

@Route(path = RConfig.MINE_UNBIND_EMAIL)//解除绑定邮箱
public class UnBindEmailActivity extends BaseActivity<UnBindEmailPresenter> {

    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.unbind_type)
    TextView unbindTel;
    @BindView(R2.id.ivBack)
    ImageView ivBack;
    @BindView(R2.id.etCode)
    EditText etCode;
    @BindView(R2.id.btSendCode)
    TextView btSendCode;
    @BindView(R2.id.btSubmit)
    Button btSubmit;
    private Disposable disposable;
    @Inject
    UserViewModel userViewModel;
    @Autowired
    public String userEmial;

    @Override
    protected int getLayoutResId() {
        return R.layout.mine_activity_un_bind;
    }

    @Override
    public UnBindEmailPresenter getPresenter() {
        return new UnBindEmailPresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        ARouter.getInstance().inject(this);//添加在onCreate（）
        DaggerUserDataComponent.create().inject(this);
        tvTitle.setText("解除绑定邮箱");
        etCode.setHint("请输入邮箱验证码");
        unbindTel.setText(userEmial);
        ClickUtil.click(ivBack, () -> {
            finish();
        });
        ClickUtil.click(btSendCode, () -> {
            if (CheckUtil.isEmail(userEmial)) {
                btSendCode.setEnabled(false);
                mPresenter.sendEmailCode(userEmial);
            }
        });
        ClickUtil.click(btSubmit, () -> {
            if (!CheckUtil.isEmail(userEmial)) {
                return;
            }
            String code = etCode.getText().toString().trim();
            if (TextUtils.isEmpty(code)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mineBindMobileActivity2));
                return;
            }
            mPresenter.unbindMobile(userEmial, code);
        });

    }

    public void sendCodeOk() {
        DisposUtil.close(disposable);
        disposable = Flowable.intervalRange(1, 60, 0, 1, TimeUnit.SECONDS)
                .map(i -> 60 - i)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(i -> {
                    btSendCode.setText(String.format(App.INSTANCE.getString(R.string.mineBindMobileActivity4), i));
                }, System.out::println, () -> {
                    btSendCode.setEnabled(true);
                    btSendCode.setText(App.INSTANCE.getString(R.string.mineBindMobileActivity5));
                });

    }

    @Override
    protected void onDestroy() {
        DisposUtil.close(disposable);
        super.onDestroy();
    }

    public void sendCodeError() {
        btSendCode.setEnabled(true);
    }

    public void onResponseSuccess() {
        userViewModel.update();
        finish();
        ARouter.getInstance().build(RConfig.MINE_BIND_EMAIL).navigation();
    }
}
